package com.zhuang.flyweight;

import java.util.HashMap;

/**
 * @Classname BoxFactory
 * @Description 工厂类 将类设计为单例模式
 * @Date 2021/3/25 15:33
 * @Created by dell
 */

public class BoxFactory {
    private HashMap<String, AbstractBox> map;

    //在构造方法中初始化
    private BoxFactory() {
        map = new HashMap<String, AbstractBox>();
        IBox iBox = new IBox();
        LBox lBox = new LBox();
        OBox oBox = new OBox();
        map.put("I", iBox);
        map.put("L", lBox);
        map.put("O", oBox);
    }

    //声明一个方法获取工厂
    public static BoxFactory getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private static class SingletonHolder {
        private static final BoxFactory INSTANCE = new BoxFactory();
    }

    //根据图形名称获取图形对象
    public AbstractBox getShape(String name) {
        return map.get(name);
    }
}
